package com.crowd.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import com.crowd.dao.DaDao;
import com.crowd.dao.UserDao;
import com.crowd.dao.ZuoDao;
import com.crowd.dao.impl.DaDaoimpl;
import com.crowd.dao.impl.UserDaoimpl;
import com.crowd.dao.impl.ZuoDaoimpl;
import com.crowd.entity.Da;
import com.crowd.entity.User;
import com.crowd.entity.Zuo;
import com.google.gson.Gson;



/**
 * Servlet implementation class Louservlet
 */
@WebServlet("/gou")
public class Louservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	UserDao dao=new UserDaoimpl();
	ZuoDao dd=new ZuoDaoimpl();
	Zuo zuo=new Zuo();
    Gson gson=new Gson();
    User u=new User();
    Da aDa=new Da();
    DaDao da=new DaDaoimpl();
   
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		String op=request.getParameter("opo");
		switch (op) {
		case "dan":
			String data=request.getParameter("da");
			
			User user=gson.fromJson(data,u.getClass());
			
			List<User> users=dao.dan(user);
			
			if (users.size()>0) {
				response.getWriter().write("true");
			}else {
				response.getWriter().write("false");
			}
			break;
		case "add":
			String dad=request.getParameter("da");
			User user1=gson.fromJson(dad,u.getClass());
			boolean addu=dao.add(user1);
			if (addu) {
				response.getWriter().write("true");
			}else {
				response.getWriter().write("false");
			}
			
			break;
		case "ad":
			String d=request.getParameter("da");
			Da a=gson.fromJson(d, aDa.getClass());
			boolean add=da.add(a);
			if (add) {
				response.getWriter().write("true");
			}else {
				response.getWriter().write("false");
			}
			break;
		case "a":
		     DaDao dao=new DaDaoimpl();
		     List<Da> das=dao.findAll();
		     String l=gson.toJson(das);
		     response.getWriter().write(l);
			System.out.println(l);
			break;
			case"addd":
				request.setCharacterEncoding("UTF-8");
				String ad=request.getParameter("ae");
				System.out.println(ad);
				Zuo zu=gson.fromJson(ad, zuo.getClass());
				
				boolean as=dd.add(zu);
				if (as) {
					response.getWriter().write("true");
				}else {
					response.getWriter().write("false");
				}
				break;
			case"find":
				List<Zuo> zuos=dd.findAll();
				String ft=gson.toJson(zuos);
				response.getWriter().write(ft);
				break;
		default:
			
		}
		
		
	}

}
